
import request from "@/utils/request";
import {QueueRoomModel} from "@/model/QueueRoomModel";
import {QueueScheduleModel} from "@/model/QueueScheduleModel";
import {QueueItemModel} from "@/model/QueueItemModel";
import {DeviceModel} from "@/model/DeviceModel";
import {DepartItemModel} from "@/model/DepartItemModel";

export default class DoctorClientApi {
    /**
     * 查询医生当前坐诊的诊室
     */
    public static getCurrentRoom(doctorId:string): Promise<{room: QueueRoomModel, device: DeviceModel, schedule: QueueScheduleModel, serverTime: number}> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/room/info`,
                method: 'get'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data ? {
                        device: res.data.device? new DeviceModel().deserialize(res.data.device) : null,
                        room: res.data.room ? new QueueRoomModel().deserialize(res.data.room) : null,
                        schedule: res.data.schedule ? new QueueScheduleModel().deserialize(res.data.schedule) : null,
                        serverTime: res.data.serverTime || new Date().getTime()
                    }: null)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static listDoctorSchedule(doctorId: string): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/schedule/today`,
                method: 'get'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data?.length ? res.data.map(item => new QueueScheduleModel().deserialize(item)) : [])
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static getItemDeptRelation(deptId: string, itemId: string): Promise<DepartItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/depart/${deptId}/item/${itemId}/relation`,
                method: 'get'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new DepartItemModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static listCurrentSchedule(doctorId: string, itemId: string, deptId: string): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/dept/${deptId}/item/${itemId}/employee`,
                method: 'get',
                params: {
                    itemId,
                    deptId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data?.length ? res.data.map(item => new QueueScheduleModel().deserialize(item)) : [])
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static changeCurrentSchedule(doctorId: string, scheduleId: string, roomId: string, deviceId: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/change/schedule/${scheduleId}`,
                method: 'POST',
                params: {
                    roomId,
                    deviceId
                }
            }).then((res: any) => {
               resolve(res)
            }, ()=> {reject()})
        })
    }

    public static changeSignQueueItem(doctorId: string, itemId: string, deptId: string, categoryId: string, targetDoctorId: string, queueItemId: string): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/change/sign`,
                method: 'put',
                params: {
                    deptId,
                    categoryId,
                    itemId,
                    targetDoctorId,
                    queueItemId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data?.length ? res.data.map(item => new QueueScheduleModel().deserialize(item)) : [])
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static listDoctorQueueMember(departId: string, itemId: string,  doctorId: string, categoryId: string, states: string): Promise<QueueItemModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/queue/member`,
                method: 'get',
                params: {
                    t:new Date().getTime(),
                    departId,
                    itemId,
                    categoryId,
                    states
                },
                showErrorMsg: false
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data?.length ? res.data.map(item => new QueueItemModel().deserialize(item)) : [])
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static callQueueMember(departId: string , itemId: string,doctorId: string, categoryId: string, roomId: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/call/queue`,
                method: 'PUT',
                params: {
                    departId,
                    itemId,
                    categoryId,
                    roomId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data ? new QueueItemModel().deserialize(res.data) : null)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static callQueueById(departId: string ,itemId: string, doctorId: string,  categoryId, queueId: string, roomId: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/call/${queueId}`,
                method: 'PUT',
                params: {
                    departId,
                    itemId,
                    categoryId,
                    roomId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static passQueueById(departId: string ,doctorId: string, itemId: string, categoryId: string, queueId: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/pass/${queueId}`,
                method: 'PUT',
                params: {
                    departId,
                    itemId,
                    categoryId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static completeQueueById(departId: string ,doctorId: string, itemId: string, categoryId: string, queueId: string): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/complete/${queueId}`,
                method: 'PUT',
                params: {
                    departId,
                    itemId,
                    categoryId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueItemModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static callQueueStatu(doctorId: string,  status: 1 | 0): Promise<QueueItemModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/call/status/${status}`,
                method: 'PUT'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static scheduleSetting(doctorId: string,  schedules: QueueScheduleModel[]): Promise<QueueScheduleModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${doctorId}/schedule/setting`,
                method: 'POST',
                data: schedules
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static listScheduleByEmplId(emplId: string, deptId: string, itemId: string, categoryId: string): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${emplId}/schedule/list`,
                method: 'get',
                params: {
                    deptId,
                    itemId,
                    categoryId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data?.map(item => new QueueScheduleModel().deserialize(item)))
                }
                reject(res);
            })
        })
    }

    public static doctorLogout(roomId: string, emplId: string): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/doctor/${emplId}/room/${roomId}/logout`,
                method: 'POST'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data)
                } else {
                    reject(res);
                }
            })
        })
    }

    public static sendDeviceHeartbeat(emplId, scheduleId, roomId, deviceCode: string): Promise<any> {
        return new Promise((resolve, reject) => {
            request({
                url: `/sse/heartbeat/connect`,
                method: 'get',
                params: {
                    emplId,
                    scheduleId,
                    roomId,
                    deviceCode
                }
            }).then((res: any) => {
                resolve(res)
            }, ()=> {reject()})
        })
    }

}

